package xx.iverson.listening_manager.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xx.iverson.listening_manager.entity.CategoryLevel1;
import xx.iverson.listening_manager.entity.CategoryLevel2;
import xx.iverson.listening_manager.result.ReturnResult;
import xx.iverson.listening_manager.service.CategoryService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * xx.iverson.listening_manager.controller.CategoryController
 * 描述：
 * on 2025/10/16-下午5:48
 *
 * @author weijx
 */
@RestController
@RequestMapping("/api/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    // 一级分类接口
    @PostMapping("/level1/list")
    public ReturnResult<List<CategoryLevel1>> getLevel1List() {
        try {
            List<CategoryLevel1> list = categoryService.getAllLevel1();
            return ReturnResult.success(list);
        } catch (Exception e) {
            return ReturnResult.error(500, "获取分类列表失败");
        }
    }

    @PostMapping("/level1/add")
    public ReturnResult<Boolean> addLevel1(@RequestBody CategoryLevel1 category) {
        return categoryService.addLevel1(category);
    }

    @PostMapping("/level1/update")
    public ReturnResult<Boolean> updateLevel1(@RequestBody CategoryLevel1 category) {
        return categoryService.updateLevel1(category);
    }

    @PostMapping("/level1/delete")
    public ReturnResult<Boolean> deleteLevel1(@RequestBody Map<String, Integer> request) {
        Integer categoryId = request.get("categoryId");
        if (categoryId == null) {
            return ReturnResult.error(400, "分类ID不能为空");
        }
        return categoryService.deleteLevel1(categoryId);
    }

    // 二级分类接口
    @PostMapping("/level2/list")
    public ReturnResult<List<CategoryLevel2>> getLevel2List() {
        try {
            List<CategoryLevel2> list = categoryService.getAllLevel2();
            return ReturnResult.success(list);
        } catch (Exception e) {
            return ReturnResult.error(500, "获取二级分类列表失败");
        }
    }

    @PostMapping("/level2/listByCategory")
    public ReturnResult<List<CategoryLevel2>> getLevel2ByCategory(@RequestBody Map<String, Integer> request) {
        try {
            Integer categoryId = request.get("categoryId");
            if (categoryId == null) {
                return ReturnResult.error(400, "一级分类ID不能为空");
            }
            List<CategoryLevel2> list = categoryService.getLevel2ByCategoryId(categoryId);
            return ReturnResult.success(list);
        } catch (Exception e) {
            return ReturnResult.error(500, "获取二级分类列表失败");
        }
    }

    @PostMapping("/level2/add")
    public ReturnResult<Boolean> addLevel2(@RequestBody CategoryLevel2 subcategory) {
        return categoryService.addLevel2(subcategory);
    }

    @PostMapping("/level2/update")
    public ReturnResult<Boolean> updateLevel2(@RequestBody CategoryLevel2 subcategory) {
        return categoryService.updateLevel2(subcategory);
    }

    @PostMapping("/level2/delete")
    public ReturnResult<Boolean> deleteLevel2(@RequestBody Map<String, Integer> request) {
        Integer subcategoryId = request.get("subcategoryId");
        if (subcategoryId == null) {
            return ReturnResult.error(400, "二级分类ID不能为空");
        }
        return categoryService.deleteLevel2(subcategoryId);
    }
}